package ConfigRCS;

require 5.004;

use Carp 'croak', 'carp';
use strict;
use warnings;
use Rcs;
use vars qw($VERSION);

$VERSION = 1.0;

sub ConfigRCS::new { 
   my ($class, $bindir, $rcsdir, $workdir) = @_; 
   my $self = { 
      _bin => undef, 
      _rcs => undef,
      _work => undef,
      _obj => undef,
      _diff => undef,
   }; 
      
   if (defined($bindir)) {
      $self->{_bin} = $bindir;
   }
   else {
      die "bin directory not defined!";
   }

   if (defined($rcsdir)) {
      if (-d $rcsdir) {
         $self->{_rcs} = $rcsdir;
      }
      else {
         warn "$rcsdir does not exist, creating it";
         opendir(RCS, $rcsdir);
         closedir(RCS);
         $self->{_rcs} = $rcsdir;
      }
   }
   else {
      die "rcs directory not defined!";
   }

   if (defined($workdir)) {
      if (-d $workdir) {
         $self->{_work} = $workdir;
      }
      else {
         warn "$workdir does not exist, creating it";
         opendir(WORK, $workdir);
         closedir(WORK);
         $self->{_work} = $workdir;
      }
   }
   else {
      die "work directory not defined!";
   }

   Rcs->bindir($self->{_bin});
   Rcs->quiet(1);
   $self->{_obj} = Rcs->new;
   $self->{_obj}->workdir($self->{_work});
   $self->{_obj}->rcsdir($self->{_rcs});

   bless ($self, $class); 
   return $self;
}

sub ConfigRCS::checkin ($$$)
{
   my (($self), $file, $message) = @_;

   if (defined($file) and -f $file) {
      if (!defined($message)) {
         my $now = localtime();
         $message = sprintf("automatic checkin of %s on %s", $file, $now);
      }

      $self->{_obj}->file($file);
      $self->{_obj}->ci("-u", "-m$message");
   }
}

sub ConfigRCS::checkout ($$$)
{
   my (($self), $file) = @_;

   if (defined($file) and -f $file) {
      $self->{_obj}->file($file);
      $self->{_obj}->co("-l");
   }
}

sub ConfigRCS::diff ($)
{
   my ($self) = @_;
   my @retval = $self->{_obj}->rcsdiff;
   return @retval;
}

1;
